---
title: Account Constraints
description: Anchor Account Constraints Examples
---

Minimal reference examples for Anchor account
[constraints](https://docs.rs/anchor-lang/latest/anchor_lang/derive.Accounts.html).

See the account constraints
[source code](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/accounts/constraints.rs)
for implementation details.

## Normal Constraints

### `#[account(signer)]`

Description: Checks the given account signed the transaction. Consider using the
Signer type if you would only have this constraint on the account.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/signer)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/signer)

```rust title="attribute"
#[account(signer)]
#[account(signer @ <custom_error>)]
```

### `#[account(mut)]`

Description: Checks the given account is mutable. Makes anchor persist any state
changes.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mut)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mut)

```rust title="attribute"
#[account(mut)]
#[account(mut @ <custom_error>)]
```

### `#[account(init)]`

Description: Creates the account via a CPI to the system program and initializes
it (sets its account discriminator).  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init)

```rust title="attribute"
#[account(
    init,
    payer = <target_account>,
    space = <num_bytes>
)]
```

### `#[account(init_if_needed)]`

Description: Same as init but only runs if the account does not exist yet.
Requires init-if-needed cargo feature.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init_if_needed)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init_if_needed)

```rust title="attribute"
#[account(
    init_if_needed,
    payer = <target_account>
)]

#[account(
    init_if_needed,
    payer = <target_account>,
    space = <num_bytes>
)]
```

### `#[account(seeds, bump)]`

Description: Checks that given account is a PDA derived from the currently
executing program, the seeds, and if provided, the bump.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/seed-bump)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/seed-bump)

```rust title="attribute"
#[account(
    seeds = <seeds>,
    bump
)]

#[account(
    seeds = <seeds>,
    bump,
    seeds::program = <expr>
)]

#[account(
    seeds = <seeds>,
    bump = <expr>
)]

#[account(
    seeds = <seeds>,
    bump = <expr>,
    seeds::program = <expr>
)]
```

### `#[account(has_one = target)]`

Description: Checks the target field on the account matches the key of the
target field in the Accounts struct.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/has_one)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/has_one)

```rust title="attribute"
#[account(
    has_one = <target_account>
)]

#[account(
    has_one = <target_account> @ <custom_error>
)]
```

### `#[account(address = expr)]`

Description: Checks the account key matches the pubkey.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/address)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/address)

```rust title="attribute"
#[account(address = <expr>)]
#[account(address = <expr> @ <custom_error>)]
```

### `#[account(owner = expr)]`

Description: Checks the account owner matches expr.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/owner)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/owner)

```rust title="attribute"
#[account(owner = <expr>)]
#[account(owner = <expr> @ <custom_error>)]
```

### `#[account(executable)]`

Description: Checks the account is executable (i.e. the account is a program).  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/executable)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/executable)

```rust title="attribute"
#[account(executable)]
```

### `#[account(zero)]`

Description: Checks the account discriminator is zero. Use for accounts larger
than 10 Kibibyte.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/zero)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/zero)

```rust title="attribute"
#[account(zero)]
```

### `#[account(close = target)]`

Description: Closes the account by sending lamports to target and resetting
data.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/close)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/close)

```rust title="attribute"
#[account(close = <target_account>)]
```

### `#[account(constraint = expr)]`

Description: Custom constraint that checks whether the given expression
evaluates to true.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/constraint)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/constraint)

```rust title="attribute"
#[account(constraint = <expr>)]
#[account(
    constraint = <expr> @ <custom_error>
)]
```

### `#[account(realloc)]`

Description: Used to realloc program account space at the beginning of an
instruction.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/realloc)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/realloc)

```rust title="attribute"
#[account(
    realloc = <space>,
    realloc::payer = <target>,
    realloc::zero = <bool>
)]
```

## SPL Constraints

### `#[account(token::*)]`

Description: Create or validate token accounts with specified mint and
authority.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token)

```rust title="attribute"
#[account(
    token::mint = <target_account>,
    token::authority = <target_account>
)]

#[account(
    token::mint = <target_account>,
    token::authority = <target_account>,
    token::token_program = <target_account>
)]
```

### `#[account(mint::*)]`

Description: Create or validate mint accounts with specified parameters.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mint)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mint)

```rust title="attribute"
#[account(
    mint::authority = <target_account>,
    mint::decimals = <expr>
)]

#[account(
    mint::authority = <target_account>,
    mint::decimals = <expr>,
    mint::freeze_authority = <target_account>
)]
```

### `#[account(associated_token::*)]`

Description: Create or validate associated token accounts.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/associated_token)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/associated_token)

```rust title="attribute"
#[account(
    associated_token::mint = <target_account>,
    associated_token::authority = <target_account>
)]

#[account(
    associated_token::mint = <target_account>,
    associated_token::authority = <target_account>,
    associated_token::token_program = <target_account>
)]
```

### `#[account(*::token_program = expr)]`

Description: The token_program can optionally be overridden.  
Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token_program)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token_program)

```rust title="attribute"
#[account(*::token_program = <target_account>)]
```

## Instruction Attribute

### `#[instruction(...)]`

Description: You can access the instruction's arguments with the
`#[instruction(..)]` attribute. You must list them in the same order as in the
instruction handler but you can omit all arguments after the last one you need.
Skipping arguments will result in an error.

Examples:
[Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/instruction)
|
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/instruction)

```rust title="snippet"
// [!code word:input]
#[program]
pub mod example {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, input: String) -> Result<()> {
        // --snip--
    }
}

#[derive(Accounts)]
// [!code highlight]
#[instruction(input: String)]
pub struct Initialize<'info> {
    #[account(
        init,
        payer = signer,
        space = 8 + 4 + input.len(),
    )]
    pub new_account: Account<'info, DataAccount>,
    // --snip--
}
```

Valid Usage:

```rust title="snippet"
// [!code word:input_one]
// [!code word:input_two]
#[program]
pub mod example {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, input_one: String, input_two: String) -> Result<()> {
        // --snip--
    }
}

#[derive(Accounts)]
// [!code highlight]
#[instruction(input_one: String, input_two: String)]
pub struct Initialize<'info> {
    // --snip--
}
```

```rust title="snippet"
// [!code word:input_one]
#[program]
pub mod example {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, input_one: String, input_two: String) -> Result<()> {
        // --snip--
    }
}

#[derive(Accounts)]
// [!code highlight]
#[instruction(input_one: String)]
pub struct Initialize<'info> {
    // --snip--
}
```

Invalid Usage, will result in an error:

```rust title="snippet"
// [!code word:input_two]
#[program]
pub mod example {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, input_one: String, input_two: String) -> Result<()> {
        // --snip--
    }
}

#[derive(Accounts)]
// [!code highlight]
#[instruction(input_two: String)]
pub struct Initialize<'info> {
    // --snip--
}
```
